package com.kingyea.app.market.supplier.service.impl;

import com.kingyea.app.market.client.UserServiceClient;
import com.kingyea.app.market.supplier.bean.Supplier;
import com.kingyea.app.market.supplier.dao.SupplierMapper;
import com.kingyea.app.market.supplier.service.SupplierService;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.Result;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description Supplier 实现类
 * @date Created in 2018-02-09 17:47:43
 */
@Service
@Transactional
public class SupplierServiceImpl implements SupplierService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierServiceImpl.class);

    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private UserServiceClient userServiceClient;

    /**
     * 通过ID查找对象
     *
     * @param supplierCode
     * @return
     */
    @Override
    public Supplier getById(Integer supplierCode) throws Exception {
        return this.supplierMapper.getById(supplierCode);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public Object list(Map<String, Object> supplier) throws Exception {
        PageInfo.startPage(supplier);
        return PageInfo.getPageInfo(supplierMapper.list(supplier));
    }

    /**
     * 删除
     *
     * @param supplierCode
     * @return
     */
    @Override
    public int deleteById(Integer supplierCode) throws Exception {
        supplierMapper.deleteById(supplierCode);
        return 1;
    }

    /**
     * 保存
     *
     * @param supplier
     * @return
     */
    @Override
    public Object save(Map<String, Object> supplier) throws Exception {
        try {
            Result<User> currentUserInfo = userServiceClient.getCurrentUserInfo();
            User data = currentUserInfo.getData();
            String userId = data.getUserId();
            String name = data.getName();
            supplier.put("createUserId", userId);
            supplier.put("createUserName", name);
            supplierMapper.save(supplier);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return supplier;
    }

    /**
     * 更新
     *
     * @param supplier
     * @return
     */
    @Override
    public Supplier update(Supplier supplier) throws Exception {
        try {
            supplierMapper.update(supplier);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return supplier;
    }

    @Override
    public Object getListBySupplier(Map<String, Object> obj) throws Exception {
        PageInfo.startPage(obj);
        return PageInfo.getPageInfo(supplierMapper.getListBySupplier(obj));
    }
}
